package com.zjff.myshop.admin.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zjff.myshop.admin.pojo.form.BatchIdsForm;
import com.zjff.myshop.admin.pojo.form.CategoryInsertForm;
import com.zjff.myshop.admin.pojo.form.CategoryUpdateForm;
import com.zjff.myshop.front.pojo.form.CategoryQueryPageForm;
import com.zjff.myshop.front.pojo.vo.CategoryVO;
import com.zjff.myshop.entity.Category;
import com.zjff.myshop.exception.BusinessException;

import com.zjff.myshop.admin.service.CategoryService;
import com.zjff.myshop.util.BeanUtils;
import com.zjff.myshop.util.PageList;
import com.zjff.myshop.util.PageQuery;
import com.zjff.myshop.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@Api(value = "v1", tags = "82.后台管理系统-分类模块")
@SaCheckRole("admin")
@RequestMapping("/admin/v1/category")
public class AdminCategoryController {
    private static final Logger logger = LoggerFactory.getLogger(AdminCategoryController.class);
    @Resource
    private CategoryService categoryService;

    /**
     * 增加
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增分类", notes = "新增分类")
    @ApiImplicitParam(name = "form", value = "新增分类参数", dataType = "CategoryInsertForm", required = true)
    @PostMapping(value = "/insert")
    public Result insert(@RequestBody @Valid @ApiParam(value = "新增分类参数", required = true) CategoryInsertForm form) {
        logger.info("新增商品分类：" + form.getName());
        Category category = new Category();
        BeanUtils.copyProperties(form, category);
        if (categoryService.insert(category)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除（单个）
     */
    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "删除单个分类", notes = "删除分类单个记录")
    @DeleteMapping(value = "/{id}")
    public Result deleteById(@PathVariable Integer id) {
        if (categoryService.deleteById(id)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 删除（批量）
     */
    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "批量删除分类", notes = "批量删除分类信息")
    @ApiImplicitParam(name = "form", value = "批量删除参数", dataType = "BatchIdsForm", required = true)
    @DeleteMapping(value = "/batch")
    public Result deleteByIds(@Valid @RequestBody BatchIdsForm form) {
        if (form == null || form.getIds().length < 1) {
            throw new BusinessException("数据不存在！");
        }

        if (categoryService.deleteBatch(form.getIds())) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 更改
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改分类", notes = "更改分类信息")
    @ApiImplicitParam(name = "form", value = "更改分类参数", dataType = "CategoryUpdateForm", required = true)
    @PostMapping(value = "/update")
    public Result update(@RequestBody @Valid CategoryUpdateForm form) {
        Category category = new Category();
        BeanUtils.copyProperties(form, category);
        if (categoryService.update(category)) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    /**
     * 查询详情
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询单个详情", notes = "根据id查询")
    @GetMapping(value = "/{id}")
    public Result info(@PathVariable("id") Integer id) {
        Category category = categoryService.selectByPrimaryKey(id);
        if (category == null) {
            throw new BusinessException("数据不存在！");
        }
        return Result.ok(category);
    }

    /**
     * 查询商品分类列表（全部）
     */
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询商品分类列表", notes = "商品分类管理页面使用")
    @GetMapping("/list")
    public Result<List<CategoryVO>> list() {
        List<CategoryVO> categoryVOList = categoryService.selectVOList();
        return Result.ok(categoryVOList);
    }

    /**
     * 查询商品分类分页列表
     */
    @ApiOperationSupport(order = 32)
    @ApiOperation(value = "查询商品分类的分页数据", notes = "商品分类管理页面使用")
    @ApiImplicitParam(name = "form", value = "查询商品分类参数", dataType = "CategoryQueryPageForm", required = true)
    @PostMapping("/queryPage")
    public Result<PageList<CategoryVO>> queryPage(@Valid @RequestBody CategoryQueryPageForm form) {
        //form:page,limit
        Map<String, Object> map = BeanUtils.beanToMap(form);
        PageQuery pageQuery = new PageQuery(map);
        return Result.ok(categoryService.selectPage(pageQuery));
    }

}